<!-- 找回密码 -->
<template>
  <div>
    <el-form
      ref="formRef"
      label-position="top"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="手机号码" prop="phone">
        <el-input
          class="countryWrapper"
          v-model="formData.phone"
          placeholder="请输入手机号码"
          clearable
        >
          <template #prefix>
            <country-code-selector :countryCode.sync="formData.countryCode" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="validateCode">
        <div class="flex-btw w-full">
          <el-input
            class="captcha"
            v-model="formData.validateCode"
            placeholder="请输入验证码"
            clearable
          ></el-input>
          <send-code class="sendCode" field="phone" :params="{ phone: formData.phone, type: 'USER_RETRIEVE' }" :elRef="formRef" />
        </div>
      </el-form-item>
      <el-form-item label="新密码" prop="pwd">
        <el-input
          v-model="formData.pwd"
          placeholder="请输入新密码"
          show-password
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPwd">
        <el-input
          v-model="formData.confirmPwd"
          placeholder="请再次输入密码"
          show-password
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="btns">
      <div>
        <el-button round class="bigBtn" type="primary" @click="onSubmit"
          >确定并返回登录</el-button
        >
      </div>
      <div>
        <el-button round class="bigBtn" @click="toggleMode('login')"
          >返回登录</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import { encryptData } from "@/utils/encrypt";
const emit = defineEmits(["update"]);

const formData = reactive({
  phone: "",
  validateCode: "",
  pwd: "",
  confirmPwd: "",
});
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== formData.pwd) {
    callback(new Error("两次密码不一致！"));
  } else {
    callback();
  }
};
const rules = {
  phone: [{ required: true, message: "请输入手机号码", trigger: "blur" }],
  pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
  validateCode: [{ required: true, message: "请输入验证码", trigger: "blur" }],
  confirmPwd: [{ required: true, validator: validatePass2, trigger: "blur" }],
};

const captcha = ref(false);

const toggleFn = () => {
  captcha.value = !captcha.value;
};

const toggleMode = (val) => {
  emit("update", val);
};

const formRef = ref();
const onSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      api.post("/user/front/retrieve", {...formData,pwd:encryptData(formData.pwd),confirmPwd:encryptData(formData.confirmPwd)}).then((res) => {
        toggleMode('login')
      });
    } else {
      console.log("error submit!");
      return false;
    }
  });
};
</script>
<style lang="scss" scoped>
.captcha {
  flex: 1;
  margin-right: 10px;
}

.toggle {
  .el-button--text {
    color: var(--use-color-warning);
    font-size: 16px;
  }
}

.mt40 {
  margin-top: 40px;
}

.btns {
  > div {
    margin-top: 24px;
  }

  .el-button {
    width: 100%;
    border-radius: 56px !important;
  }
}

.sendCode{
  height: 56px;
  border-radius:28px;
}
</style>
